import 'package:cbec_app/entity/pay_method/installment_model.dart';

class PayTypeModel {
  final String name;
  final String code;
  final String tag;
  final bool hasInstallment;
  final List<Installment>? installment;
  final String icon; // 新增的属性
  bool expend;

  PayTypeModel({
    required this.name,
    required this.code,
    required this.tag,
    required this.hasInstallment,
    this.installment,
    required this.icon, // 确保此处包含icon参数
    required this.expend,
  });

  factory PayTypeModel.fromJson(Map<String, dynamic> json) {
    // 根据 code 设置图标
    String icon;
    switch (json['code']) {
      case 'wechat_pay':
        icon = 'wx.png';
        break;
      case 'ali_pay':
        icon = 'zfb.png';
        break;
      case 'union_pay':
        icon = 'ysf.png'; // 替换为适当的图标名称
        break;
      case 'ysf_pay':
        icon = 'ysf.png'; // 替换为适当的图标名称
        break;
      default:
        icon = 'default.png'; // 默认图标
        break;
    }

    return PayTypeModel(
      name: json['name'],
      code: json['code'],
      tag: json['tag'],
      hasInstallment: json['hasInstallment'],
      installment: json['installment'] != null ? (json['installment'] as List).map((i) => Installment.fromJson(i)).toList() : null,
      icon: icon, // 使用计算得出的图标
      expend: false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'code': code,
      'tag': tag,
      'hasInstallment': hasInstallment,
      'installment': installment?.map((i) => i.toJson()).toList(),
      'icon': icon, // 如果需要，可以将 icon 也加入到 toJson
    };
  }
}
